<?php
use system\core\Config;

/**
 * Convert to the current currency
 * 
 * It will automatically convert based on your language set in settings.ini.
 * 
 * If you want you can remove the space between the currency sign and the number, in
 * this case, set $space to FALSE (defaults to TRUE).
 * 
 * @param int $number
 * @param int $decimals 2
 * @param boolean $space TRUE
 * @return string
 */
function currency( $number, $decimals = 2, $space = TRUE )
{
    // Just return the number if it is not numeric, no need to do anything...
    if ( ! is_numeric($number) )
    {
        return $number;
    }
    
    $language = Config::get('translation', 'language');
    
    // Get the languages array
    include SYS_PATH . 'language/languages.php';
    
    // The language is supported
    if ( array_key_exists($language, $languages) )
    {
        if ( $languages[$language]['1'] === TRUE )
        {
            return ( $space === TRUE ) 
                    ? $languages[$language]['0'] . ' ' . number_format($number, $decimals, '.', ',')
                    : $languages[$language]['0'] . number_format($number, $decimals, '.', ',');
        }
        else
        {
            return ( $space === TRUE ) 
                    ? $languages[$language]['0'] . ' ' . number_format($number, $decimals, ',', '.')
                    : $languages[$language]['0'] . number_format($number, $decimals, ',', '.');
        }
    }
    
    // The language is not supported, just use the en_US one.
    return ( $space === TRUE ) 
            ? $languages['en_US']['0'] . ' ' . number_format($number, $decimals, '.', ',')
            : $languages['en_US']['0'] . number_format($number, $decimals, '.', ',');
}

/* End of file currency_helper.php */